package com.baidu.health.utils;

import com.alibaba.fastjson.JSON;
import com.baidu.health.pojo.TbArticle;
import com.baidu.health.pojo.TbSensitivity;
import com.baidu.health.service.TbArticleServiceImpl;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 敏感词检测
 * @author 崔先生
 * @version 1.0.0
 * @date 2022/03/24
 * ckassName java.lang.String
 */
public class KeyWordFilter {

	@Autowired
	RestHighLevelClient restHighLevelClient;

	@Autowired
	TbArticleServiceImpl articleService;

	private static Pattern pattern = null;
	private static int keywordsCount = 0;

	public boolean doFilter(String str) throws Exception{
		// 从words.properties初始化正则表达式字符串
		StringBuffer patternBuffer = new StringBuffer();

//		SearchRequest request = new SearchRequest("min");
//		SearchSourceBuilder builder = new SearchSourceBuilder();
//		builder.query(QueryBuilders.matchAllQuery());
//		builder.size(5000);
//		request.source(builder);
//		SearchHit[] hits = restHighLevelClient.search(request, RequestOptions.DEFAULT).getHits().getHits();
//		List<TbSensitivity> list = new ArrayList<>();
//		for (SearchHit hit : hits) {
//			TbSensitivity parseObject = JSON.parseObject(hit.getSourceAsString(), TbSensitivity.class);
//			list.add(parseObject);
//		}
//		patternBuffer.append("(");
//		Iterator<TbSensitivity> iterator = list.iterator();
//		while( iterator.hasNext() ){
//			String scontent = iterator.next().getSensitivityContent();
//			patternBuffer.append(scontent + "|");
//			keywordsCount ++;
//		}

//		List<TbSensitivity> list = articleService.getTbSensitivityList();

		//words.properties
			InputStream in = KeyWordFilter.class.getClassLoader().getResourceAsStream("keywords.properties");
			Properties property = new Properties();
			property.load(in);
			Enumeration<?> enu = property.propertyNames();
			patternBuffer.append("(");
			while (enu.hasMoreElements()) {
				String scontent = (String) enu.nextElement();
				patternBuffer.append(scontent + "|");
				keywordsCount ++;
			}

			patternBuffer.deleteCharAt(patternBuffer.length() - 1);
			patternBuffer.append(")");
			pattern = Pattern.compile(patternBuffer.toString());
		boolean checkMsg = true ;

		Matcher m = pattern.matcher(str);
		while (m.find()) {// 查找符合pattern的字符串
//			System.out.println("The result is here :" + m.group());
			if ( m.group() != null ){
				checkMsg = false ;
			}
		}
		// 选择替换方式，这里以* 号代替
//		str = m.replaceAll("*");
		return checkMsg;
	}


//	public List<TbSensitivity> getTbSensitivityList() throws Exception{
//		SearchRequest request = new SearchRequest("min");
//		SearchSourceBuilder builder = new SearchSourceBuilder();
//		builder.query(QueryBuilders.matchAllQuery());
//		builder.size(5000);
//		request.source(builder);
//		SearchHit[] hits = restHighLevelClient.search(request, RequestOptions.DEFAULT).getHits().getHits();
//		List<TbSensitivity> list = new ArrayList<>();
//		for (SearchHit hit : hits) {
//			TbSensitivity parseObject = JSON.parseObject(hit.getSourceAsString(), TbSensitivity.class);
//			list.add(parseObject);
//		}
//		return list;
//	}

}
